package com.chinda.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.chinda.common.annotation.SysLog;
import com.chinda.common.enums.MessageEnum;
import com.chinda.common.exception.RRException;
import com.chinda.common.utils.R;
import com.chinda.modules.sys.dto.SysRoleDTO;
import com.chinda.modules.sys.entity.SysRoleEntity;
import com.chinda.modules.sys.qo.SysRoleQO;
import com.chinda.modules.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 系统角色
 *
 * @author chinda
 * @date 2021-06-22 06:07:59
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sysroles")
@Api(tags = "系统角色")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     * 分页
     *
     * @param qo
     * @return
     */
    @SysLog("查询系统角色分页")
    @PostMapping("/page")
    @ApiOperation(value = "查询分页信息")
    public R page(@ApiParam(value = "qo", name = "查询实例", required = true) @RequestBody SysRoleQO qo) {
        return R.ok().set(sysRoleService.page(qo));
    }

    /**
     * 列表
     *
     * @param qo
     * @return
     */
    @SysLog("查询系统角色列表")
    @PostMapping("/list")
    @ApiOperation(value = "查询列表信息")
    public R list(@RequestBody SysRoleQO qo) {
        return R.ok().set(sysRoleService.list(qo.getQueryWrapper()));
    }

    /**
     * 获取详情
     *
     * @return
     */
    @SysLog("查询系统角色详情")
    @GetMapping("/{roleId}")
    @ApiOperation(value = "获取详情" )
    public R info(@PathVariable("roleId")  Long roleId) {
        SysRoleEntity sysRole = sysRoleService.getById(roleId);
        return R.ok().set(sysRole);
    }

    /**
     * 保存
     *
     * @return
     */
    @SysLog("保存系统角色")
    @PostMapping
    @ApiOperation(value = "保存信息")
    public R save(@ApiParam(value = "sysRoleDTO", name = "系统角色DTO实例", required = true) @RequestBody @Valid SysRoleDTO sysRoleDTO) {
        SysRoleEntity sysRole = new SysRoleEntity();
        BeanUtil.copyProperties(sysRoleDTO, sysRole, CopyOptions.create().ignoreNullValue());
        sysRoleService.save(sysRole);
        return R.ok().set(sysRole);
    }

    /**
     * 修改全属性
     *
     * @param sysRoleDTO
     * @return
     */
    @SysLog("修改系统角色全属性")
    @PutMapping("/{roleId}")
    @ApiOperation(value = "修改全属性")
    public R modify(@PathVariable("roleId") Long roleId,
                    @ApiParam(value = "sysRoleDTO", name = "系统角色DTO实例", required = true) @RequestBody @Valid SysRoleDTO sysRoleDTO) {
        SysRoleEntity sysRole = new SysRoleEntity();
        BeanUtil.copyProperties(sysRoleDTO, sysRole, CopyOptions.create().ignoreNullValue());
        sysRole.setRoleId(roleId);
        if (!sysRoleService.updateById(sysRole)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(sysRole);
    }

    /**
     * 修改局部属性
     *
     * @param roleId
     * @param sysRoleDTO
     * @return
     */
    @SysLog("修改系统角色局部属性")
    @PatchMapping("/{roleId}")
    @ApiOperation(value = "修改局部属性")
    public R patch(@PathVariable("roleId") Long roleId,
                   @ApiParam(value = "sysRoleDTO", name = "系统角色DTO实例", required = true) @RequestBody @Valid SysRoleDTO sysRoleDTO) {
        SysRoleEntity dbSysRole = sysRoleService.getById(roleId);
        if (dbSysRole == null) {
            throw new RRException(MessageEnum.SYS_CHECK_0022.getMsg());
        }
        BeanUtil.copyProperties(sysRoleDTO, dbSysRole, CopyOptions.create().ignoreNullValue());
        if (!sysRoleService.updateById(dbSysRole)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(dbSysRole);
    }

    /**
     * 删除数据
     *
     * @param roleId
     * @return
     */
    @SysLog("删除系统角色")
    @DeleteMapping("/{roleId}")
    @ApiOperation(value = "删除数据")
    public R remove(@PathVariable("roleId") Long roleId) {
        sysRoleService.removeById(roleId);
        return R.ok();
    }
    /**
     * 批量删除数据
     *
     * @param roleIds
     * @return
     */
    @SysLog("批量删除系统角色")
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除数据")
    public R remove(@ApiParam(value = "roleIds", name = "roleId集合", required = true) @RequestBody Long [] roleIds) {
        sysRoleService.removeByIds(Arrays.asList(roleIds));
        return R.ok();
    }
}
